package com.mengdie.shuidi.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.manager.MDAccountMgr;
import com.mengdie.shuidi.manager.MDUserMgr;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.model.ExceedUserEnetity;
import com.mengdie.shuidi.ui.adapter.CoilAdapter;
import com.mengdie.shuidi.utils.ToastUtil;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


/**
 * 作者：HQY on 17/2/22 15:53
 * 邮箱：hqy_xz@126.com
 */

public class TerminalActivity extends BaseActivity {


    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.rv_terminal)
    RecyclerView mRvTerminal;


    private List<ExceedUserEnetity> exceedUserList;
    private CoilAdapter mCoilAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminal);
        ButterKnife.bind(this);

        init();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getTermianl();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void init() {
        mTvGenericTitle.setText("在线终端");
        mCoilAdapter = new CoilAdapter(exceedUserList,"2");
        mRvTerminal.setLayoutManager(new LinearLayoutManager(TerminalActivity.this));
        mRvTerminal.setAdapter(mCoilAdapter);
        mRvTerminal.addOnItemTouchListener(new OnItemClickListener() {


            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                clearSession(exceedUserList.get(position));
            }

            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private void getTermianl() {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_id", MDAccountMgr.getInstance().getSessionId());
        map.put("username", MDUserMgr.getInstance().getUsername());
        JSONObject jsonObject = new JSONObject(map);
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"account/online_list")
                .tag(this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();

                        Type exceed = new TypeToken<BaseModel<List<ExceedUserEnetity>>>() {
                        }.getType();
                        BaseModel<List<ExceedUserEnetity>> exceeModel;
                        try {
                            exceeModel = gson.fromJson(s, exceed);
                        } catch (JsonSyntaxException e) {
                            exceeModel = gson.fromJson(s, BaseModel.class);
                        }
                        if (exceeModel.getRet().equals("0")) {
                            exceedUserList = exceeModel.getData();
                            mCoilAdapter.setNewData(exceedUserList);

                        } else {
                            ToastUtil.showToast(exceeModel.getMsg());
                        }
                    }
                });

    }

    @OnClick(R.id.rl_generic_back)
    public void onClick() {
        onBackPressed();
    }

    private void clearSession(ExceedUserEnetity userInfo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_id", userInfo.getSession_id());
        map.put("username", userInfo.getUsername());
        JSONObject jsonObject = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"client/clear_session")
                .tag(this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            getTermianl();
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });

    }
}
